package com.hghivln.erpwarehouse.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.business.entity.Provider;
import com.hghivln.erpwarehouse.business.mapper.ProviderMapper;
import com.hghivln.erpwarehouse.business.service.ProviderService;
import com.hghivln.erpwarehouse.business.vo.ProviderVo;
import com.hghivln.erpwarehouse.system.common.lang.Constant;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-07-10
 */
@Service
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements ProviderService {

    @Override
    public DataGridlView QueryAllProvider(ProviderVo providerVo) {
        Page<Provider> page = new Page<>(providerVo.getPage(),providerVo.getLimit());
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.like(!StringUtils.isEmpty(providerVo.getProvidername()),"providername",providerVo.getProvidername());
        qw.like(!StringUtils.isEmpty(providerVo.getConnectionperson()),"connectionperson",providerVo.getConnectionperson());
        qw.like(!StringUtils.isEmpty(providerVo.getPhone()),"telephone",providerVo.getPhone());
        getBaseMapper().selectPage(page, qw);
        return new DataGridlView(page.getTotal(),page.getRecords());
    }

    @CachePut(cacheNames = "com.hghivln.erpwarehouse.business.service.impl.ProviderServiceImpl",key = "result.id")
    @Override
    public Provider addProvider(Provider provider) {
        getBaseMapper().insert(provider);
        return provider;
    }

    @CachePut(cacheNames = "com.hghivln.erpwarehouse.business.service.impl.ProviderServiceImpl",key = "#result.id")
    @Override
    public Provider updateProvider(Provider provider) {
        getBaseMapper().updateById(provider);
        return provider;
    }

    @Override
    public DataGridlView getAllAvailableProvider() {
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        List<Provider> list = getBaseMapper().selectList(qw);
        return new DataGridlView(list);
    }

    @CacheEvict(cacheNames = "com.hghivln.erpwarehouse.business.service.impl.ProviderServiceImpl",key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    @CachePut(value = "com.hghivln.erpwarehouse.business.service.impl.ProviderServiceImpl",key = "#id")
    @Override
    public Provider getById(Serializable id) {
        return super.getById(id);
    }
}
